<?php

namespace app\components;

use app\models\IpPackage4auto;
use Redis;
use yii\base\Component;
use Yii;

class ChinaService extends Component
{
    public static function isChina()
    {
        $is_china = 1;

        $ip_addr = Yii::$app->request->userIP ?? '123.160.170.242' ;

        if ($ip_addr == '127.0.0.1' || $ip_addr == '10.0.3.210') {
            return $is_china;
        }

        $ip = bindec(decbin(ip2long($ip_addr)));
        $ipAddr = IpPackage4auto::find()->where("$ip>start_ip and $ip<end_ip")->asArray()->one();

        $china_area_group = [
            '华东',
            '华西',
            '华南',
            '华北',
            '华中',
            '东北',
            '西南',
            '西北',
        ];
        $area_group = $ipAddr['area_group'] ? $ipAddr['area_group'] : '华北';
        if (in_array($area_group, $china_area_group)) {
            $is_china = 1;
        } else {
            $is_china = 0;
        }
        return $is_china;
    }

    public static function isPov(){
        $is_pov = 0;
        //使用新IP库
        return 0;
        $ip = self::getClientIp();
        if( $ip != '127.0.0.1'){
            $ip = ip2long($ip);
            $redis = new Redis();
            $redis->connect('r-wz997cb7cd25a924.redis.rds.aliyuncs.com', '6379');
            $redis->auth('TIM2bKqrfyMwsulm');
            $redis->select(3);
            $zKey = 'l0ipselect';
            $resKey = $redis->zrangebyscore($zKey, $ip, '+inf', array('limit'=>array(0,1)));
            if ($resKey) {
                $res = $redis->hget($resKey[0], 'content');
            }
            $ipInfo = json_decode($res, true);
            $province = $ipInfo['province'];
            //使用结束
            $is_pov = 0;
            $povArray = ['北京'];
            if( in_array($province, $povArray) ){
                $is_pov = 1;
            }
        }
        return $is_pov;
    }


    public static function getClientIp()
    {
        // 得到CDN后面的真实IP
        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '127.0.0.1';
        if ($_SERVER['HTTP_X_FORWARDED_FOR']) {
            if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ",") !== false) {
                $tmp = explode(",", $_SERVER['HTTP_X_FORWARDED_FOR']);
                $ip = @trim(@array_shift($tmp));
                if (!$ip || $ip == 'unknown') {
                    $ip = @trim(@array_shift($tmp));
                }
            } else {
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            }
        }
        if ($_SERVER['HTTP_CDN_SRC_IP']) {
            // cdn转发多ip的问题
            if (strpos($_SERVER['HTTP_CDN_SRC_IP'], ",") !== false) {
                $tmp = explode(",", $_SERVER['HTTP_CDN_SRC_IP']);
                $ip = @trim(@array_shift($tmp));
                if (!$ip || $ip == 'unknown') {
                    $ip = @trim(@array_shift($tmp));
                }
            } else {
                $ip = $_SERVER['HTTP_CDN_SRC_IP'];
            }
        }
        //akamai重设cdn后地址
        if ($_SERVER['HTTP_TRUE_CLIENT_IP']) {
            if (strpos($_SERVER['HTTP_TRUE_CLIENT_IP'], ",") !== false) {
                $tmp = explode(",", $_SERVER['HTTP_TRUE_CLIENT_IP']);
                $ip = @trim(@array_shift($tmp));
                if (!$ip || $ip == 'unknown') {
                    $ip = @trim(@array_shift($tmp));
                }
            } else {
                $ip = $_SERVER['HTTP_TRUE_CLIENT_IP'];
            }
        }
        //last 如果为内网，使用REMOTE_ADDR
        return $ip;
    }

    public static function getIpPov($ip)
    {
        $new = null;
        $old = null;
        $oip = $ip;
        $ip = ip2long($ip);
        $redis = new Redis();
        $redis->connect('r-wz997cb7cd25a924.redis.rds.aliyuncs.com', '6379');
        $redis->auth('TIM2bKqrfyMwsulm');
        $redis->select(3);
        $zKey = 'l0ipselect';
        $resKey = $redis->zrangebyscore($zKey, $ip, '+inf', array('limit'=>array(0,1)));
        if ($resKey) {
            $res = $redis->hget($resKey[0], 'content');
            $new = json_decode($res, true);
        }

        $old = Service::factory('IpPackage4AutoService')->getAreaByIP($oip);

        return [
            'new' => $new,
            'old' => $old,
        ];

    }


}
